import java.util.ArrayList;


public class TransactionList {

    /* ======================================================
     * MEMBERS
     * ====================================================== */
    private ArrayList<Transaction> _list;
    private ArrayList<Transaction> _searchList;
    
    /* ======================================================
     * CONSTRUCTOR
     * ====================================================== */
    public TransactionList(){
	_list = new ArrayList<Transaction>();
	_searchList = new ArrayList<Transaction>();
    }
    
    /* ======================================================
     * METHODS
     * ====================================================== */
    public void add(Transaction t){
	_list.add(t);
    }
    
    public ItemsetList getLarge1Itemsets(Double support){
	ItemsetList _itemsetlist = new ItemsetList(support, this, 1);
	// Go through each transaction and isolate each item to create the 
	// 1-itemset list
	for (Transaction t : _list){
	    for (String s : t.getItems()){
		Itemset iset = new Itemset(s);
		if (_itemsetlist.contains(iset)){
		   _itemsetlist.get(iset).addTransaction(t);
		}
		else {
		    iset.addTransaction(t);
		    _itemsetlist.add(iset);
		}
	    }
	}
	
	// Calculate the support for each itemset
	_itemsetlist.calculateSupport();
	
	// prune the itemsets that don't meet the support
	_itemsetlist.prune();
	
	// add to _searchList the transactions that contain the large itemsets. This way
	// we don't have to traverse the full list of transactions each time we go to increment
	// the transaction counts of candidates
	for (String key : _itemsetlist.keys()){
	    for (Transaction t : _itemsetlist.get(key).getTransactions()){
		if (!_searchList.contains(t)){
		    _searchList.add(t);
		}
	    }
	}
	
	return _itemsetlist;
    }
   
    public ArrayList<Transaction> getTransactions(){
	return _searchList;
    }
    
    public void print(){
	for (Transaction t : _list){
	    t.print();
	}
    }
    
    public Integer size() {
	return _list.size();
    }
}
